package com.bosssoft.service.impl;


import com.bosssoft.entity.dto.FibBudgetDTO;
import com.bosssoft.entity.po.FibBudgetPO;
import com.bosssoft.entity.vo.FibBudgetVO;
import com.bosssoft.enums.BgtCode;
import com.bosssoft.exception.DaoException;
import com.bosssoft.model.BudgetExcelModel;
import com.bosssoft.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibBudgetMapper;
import com.bosssoft.service.FibBudgetService;

import javax.annotation.Resource;

/**
 * @author Li kexi
 * Created on 2021/8/19.15:46
 */
@Service
@Slf4j
public class FibBudgetServiceImpl extends ServiceImpl<FibBudgetMapper, FibBudgetPO> implements FibBudgetService{

    @Resource
    private FibBudgetMapper fibBudgetMapper;

    @Override
    public FibBudgetDTO selectById(Long bgtId) {
        try {
            FibBudgetPO fibBudgetPO = this.baseMapper.selectById(bgtId);
            FibBudgetDTO budgetDTO = new FibBudgetDTO();
            if (fibBudgetPO == null) {
                return null;
            }
            BeanUtils.copyProperties(fibBudgetPO, budgetDTO);
            return budgetDTO;
        } catch (Exception e) {
            log.info("fib_budget预算的selectById出错啦!");
            throw DaoException.error(BgtCode.CANT_FOUND_RECORD);
        }
    }

    @Override
    public int updateBatch(List<FibBudgetPO> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<FibBudgetPO> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<FibBudgetPO> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(FibBudgetPO record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(FibBudgetPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public boolean export(String filePath, List<FibBudgetVO> table) {

        ArrayList<BudgetExcelModel> data = new ArrayList<>();
        int size = table.size();
        log.info("开始导出数据");
        for (int i = 0; i < size; i++) {
            BudgetExcelModel budgetExcelModel = new BudgetExcelModel();

            budgetExcelModel.setBgtDepartment(table.get(i).getBgtDepartment());
            budgetExcelModel.setBgtType(table.get(i).getBgtType());
            budgetExcelModel.setBgtReason(table.get(i).getBgtReason());
            budgetExcelModel.setBgtAmount(table.get(i).getBgtAmount());
            budgetExcelModel.setBgtStatus(table.get(i).getBgtStatus());
            budgetExcelModel.setCreateBy(table.get(i).getCreateBy());

            data.add(budgetExcelModel);
        }
        log.info("开始写入文件");
        try {
            ExcelUtil.writeWithTemplate(filePath, data);
            return true;
        } catch (DaoException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean createFromLastCheck(Map<Long, BigDecimal> mapForDeptId) {

        for(Long key : mapForDeptId.keySet()){
            FibBudgetPO fibBudgetPO = new FibBudgetPO();
            fibBudgetPO.setBgtDepartment(key);
            fibBudgetPO.setBgtAmount(mapForDeptId.get(key));
            fibBudgetPO.setCreateTime(new Date());

            fibBudgetMapper.insert(fibBudgetPO);
        }
        return true;
    }

}
